﻿using System;
using System.Collections.Generic;
using System.Linq;
using Monkey.Framework.Logging;
using Monkey.Warrenty.Data;
using Monkey.Warrenty.Data.Interfaces;
using Monkey.Warrenty.EntityModel;

namespace Monkey.Warrenty.Biz
{
    public class CustomerBiz
    {
        #region Properties

        private readonly ICustomerGroupDao _customerGroupDao;
        private readonly ICustomerDao _customerDao;

        #endregion

        #region Constructors

        public CustomerBiz()
        {
            _customerGroupDao = new CustomerGroupDao();
            _customerDao = new CustomerDao();
        }

        public CustomerBiz(ICustomerGroupDao customerGroupDao, ICustomerDao customerDao)
        {
            _customerGroupDao = customerGroupDao;
            _customerDao = customerDao;
        }

        #endregion

        #region Methods

        public List<CustomerGroup> GetAllCustomerGroups()
        {
            try
            {
                return _customerGroupDao.GetAllCustomerGroups();
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
            }
            return new List<CustomerGroup>();
        }

        public object GetCustomerGroupOptions()
        {
            try
            {
                var groups = _customerGroupDao.GetAllCustomerGroups();//.Select(g => new { DisplayText = g.Name, Value = g.Id });
                groups.Insert(0, new CustomerGroup { Id = 0, Name = string.Empty });
                return new { Result = "OK", Options = groups.Select(g => new { DisplayText = g.Name, Value = g.Id }) };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object CustomerListByFilter(string name, int customerGroupId, int jtStartIndex, int jtPageSize, string jtSorting)
        {
            try
            {
                //Get data from database
                var customerCount = _customerDao.GetCustomerCountByFilter(name, customerGroupId);
                var customers = _customerDao.GetCustomersByFilter(name, customerGroupId, jtStartIndex, jtPageSize, jtSorting);

                //Return result to jTable
                return new { Result = "OK", Records = customers, TotalRecordCount = customerCount };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public Customer GetById(int id)
        {
            if (id <= 0) return null;
            try
            {
                return _customerDao.GetById(id);
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
            }

            return null;
        }

        public Customer CreateCustomer(Customer customer)
        {
            if (customer == null) return null;
            if (string.IsNullOrEmpty(customer.Code) || string.IsNullOrEmpty(customer.Name)) return null;

            try
            {
                var addedCustomer = _customerDao.AddCustomer(customer);
                return addedCustomer;
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
                return null;
            }
        }

        public Customer UpdateCustomer(Customer customer)
        {
            if (customer == null) return null;

            try
            {
                _customerDao.UpdateCustomer(customer);
                return customer;
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
                return null;
            }
        }

        public object CreateCustomerWs(Customer customer)
        {
            if (customer == null) return null;
            if (string.IsNullOrEmpty(customer.Code) || string.IsNullOrEmpty(customer.Name)) return null;

            try
            {
                var addedCustomer = _customerDao.AddCustomer(customer);
                return new { Result = "OK", Record = addedCustomer };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object UpdateCustomerWs(Customer customer)
        {
            if (customer == null) return null;

            try
            {
                _customerDao.UpdateCustomer(customer);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object DeleteCustomer(int customerId)
        {
            try
            {
                _customerDao.DeleteCustomer(customerId);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public int GetMaxId()
        {
            return _customerDao.GetMaxId();
        }

        public int GetNextCode()
        {
            return _customerDao.GetCurrentIdentity() + 1;
        }

        #endregion

        #region Helpers
        #endregion
    }
}
